package com.cleaningcloud.cld.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.cleaningcloud.cld.api.domain.CldStaff;
import com.cleaningcloud.cld.domain.CldNoticeConfirm;
import com.cleaningcloud.cld.domain.CldProjectPost;
import com.cleaningcloud.cld.domain.dto.CldProjectPostDto;
import com.cleaningcloud.cld.domain.vo.CldProjectPostSetVo;
import com.cleaningcloud.cld.domain.vo.CldProjectPostVo;
import com.cleaningcloud.cld.domain.vo.CldStaffVo;
import com.cleaningcloud.cld.mapper.CldNoticeConfirmMapper;
import com.cleaningcloud.cld.mapper.CldProjectPostMapper;
import com.cleaningcloud.cld.mapper.CldStaffMapper;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.utils.StringUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cleaningcloud.cld.mapper.CldNoticeMapper;
import com.cleaningcloud.cld.domain.CldNotice;
import com.cleaningcloud.cld.service.ICldNoticeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通知公告Service业务层处理
 * 
 * @author cleaningcloud
 * @date 2024-10-27
 */
@Service
public class CldNoticeServiceImpl implements ICldNoticeService 
{
    @Autowired
    private CldNoticeMapper cldNoticeMapper;

    @Autowired
    private CldStaffMapper cldStaffMapper;

    @Autowired
    private CldNoticeConfirmMapper noticeConfirmMapper;

    @Autowired
    private CldProjectPostMapper cldProjectPostMapper;

    /**
     * 查询通知公告
     * 
     * @param noticeId 通知公告主键
     * @return 通知公告
     */
    @Override
    public CldNotice selectCldNoticeByNoticeId(Long noticeId)
    {
        return cldNoticeMapper.selectCldNoticeByNoticeId(noticeId);
    }

    /**
     * 查询通知公告列表
     * 
     * @param cldNotice 通知公告
     * @return 通知公告
     */
    @Override
    public List<CldNotice> selectCldNoticeList(CldNotice cldNotice)
    {
        return cldNoticeMapper.selectCldNoticeList(cldNotice);
    }

    /**
     * 新增通知公告
     * 
     * @param cldNotice 通知公告
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCldNotice(CldNotice cldNotice)
    {
        cldNotice.setCreateTime(DateUtils.getNowDate());
        cldNotice.setCreateId(SecurityUtils.getUserId());
        cldNotice.setCreateName(SecurityUtils.getUsername());
        cldNotice.setFlag(0L);
        cldNoticeMapper.insertCldNotice(cldNotice);
        // 通知公共确认表
        List<CldNoticeConfirm> confirmList = new ArrayList<>();
        CldProjectPostDto query = new CldProjectPostDto();
        if(StringUtils.isNotEmpty(cldNotice.getProjectIds()) && StringUtils.isNotEmpty(cldNotice.getDuits())){
            query.getParams().put("projectIds", cldNotice.getProjectIds());
            query.getParams().put("duits", cldNotice.getDuits());
            query.setIsMasterDuits(1L);
            query.getParams().put("isNotNullStaffId",1);
            List<CldProjectPostSetVo> list = cldProjectPostMapper.selectCldProjectPostList(query);
            for (CldProjectPostSetVo projectPostSetVo : list) {
                CldNoticeConfirm cldNoticeConfirm = new CldNoticeConfirm();
                cldNoticeConfirm.setNoticeId(cldNotice.getNoticeId());
                cldNoticeConfirm.setStaffId(projectPostSetVo.getStaffId());
                cldNoticeConfirm.setProjectId(projectPostSetVo.getProjectId());
                cldNoticeConfirm.setIsConfirm(0L);
                confirmList.add(cldNoticeConfirm);
            }
        }else{
            // 查询已入职 已在岗员工
            CldStaff cldStaffQuery = new CldStaff();
            cldStaffQuery.getParams().put("staffStatuss", "1,2");
            List<CldStaffVo> cldStaffList = cldStaffMapper.selectCldStaffList(cldStaffQuery);
            for (CldStaffVo cldStaffVo : cldStaffList) {
                CldNoticeConfirm cldNoticeConfirm = new CldNoticeConfirm();
                cldNoticeConfirm.setNoticeId(cldNotice.getNoticeId());
                cldNoticeConfirm.setStaffId(cldStaffVo.getStaffId());
                cldNoticeConfirm.setProjectId(cldStaffVo.getProjectId());
                cldNoticeConfirm.setIsConfirm(0L);
                confirmList.add(cldNoticeConfirm);
            }
        }
        if(!confirmList.isEmpty()){
            noticeConfirmMapper.batchCldNoticeConfirm(confirmList);
        }
        return 1;
    }

    /**
     * 修改通知公告
     * 
     * @param cldNotice 通知公告
     * @return 结果
     */
    @Override
    public int updateCldNotice(CldNotice cldNotice)
    {
        return cldNoticeMapper.updateCldNotice(cldNotice);
    }

    /**
     * 批量删除通知公告
     * 
     * @param noticeIds 需要删除的通知公告主键
     * @return 结果
     */
    @Override
    public int deleteCldNoticeByNoticeIds(Long[] noticeIds)
    {
        return cldNoticeMapper.deleteCldNoticeByNoticeIds(noticeIds);
    }

    /**
     * 删除通知公告信息
     * 
     * @param noticeId 通知公告主键
     * @return 结果
     */
    @Override
    public int deleteCldNoticeByNoticeId(Long noticeId)
    {
        return cldNoticeMapper.deleteCldNoticeByNoticeId(noticeId);
    }

    /**
     * @Author Shaw
     * @Description 根据通知公告ID获取确认明细列表
     * @date: 2025-03-18 16:58
     * @param: //TODO
     */
    @Override
    public List<CldNoticeConfirm> getNoticeConfirmList(CldNoticeConfirm cldNoticeConfirm){
        return noticeConfirmMapper.getNoticeConfirmList(cldNoticeConfirm);
    };
}
